package com.jiwei.community.schedule;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jiwei.community.entity.Community;
import com.jiwei.community.entity.UserPostScore;
import com.jiwei.community.service.CommunityService;
import com.jiwei.community.service.PostService;
import com.jiwei.community.service.UserPostScoreService;
import com.jiwei.community.service.impl.RecommendServiceImpl;
import com.jiwei.community.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 季伟
 * @date 2024/4/7
 */
@Component
public class updateSimilarityCache {
    @Autowired
    RecommendServiceImpl recommendService;
    @Autowired
    UserPostScoreService userPostScoreService;
    @Autowired
    CommunityService communityService;
    @Autowired
    PostService postService;
    @Autowired
    RedisCache redisCache;
    @Autowired
    RedisTemplate redisTemplate;
    @Transactional
    @Scheduled(cron = "0 0 * * * ?")//每小时执行
    public void updateSimilarityCache() {//首先将缓存中的每个用户对各个帖子的评分数据更新到数据库中，然后在根据数据库评分数据计算所有用户各自的相似度用户缓存到redis

//        userPostScoreService.updateBatchById(list);
        Set<String> keys = redisTemplate.keys("recommend:score*");//获取所有用户评分zset的键
        List<UserPostScore> userPostScoreList = new LinkedList<>();
        keys.forEach(key->{

            Long userId = Long.parseLong(key.split(":")[2]);
            Set<ZSetOperations.TypedTuple<Long>> scores = redisTemplate.opsForZSet().rangeWithScores(key,0,-1);//获取该用户的所有正反馈记录
            for (ZSetOperations.TypedTuple<Long> score : scores){//遍历所有正反馈记录将用户对帖子的评分更新到数据库中
                LambdaUpdateWrapper<UserPostScore> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.set(UserPostScore::getScore,score.getScore());
                updateWrapper.eq(UserPostScore::getUserId,userId);
                updateWrapper.eq(UserPostScore::getPostId,score.getValue());
                UserPostScore userPostScore = new UserPostScore();
                userPostScore.setScore(score.getScore());
                userPostScore.setUserId(userId);
                userPostScore.setPostId(score.getValue());
                userPostScoreService.saveOrUpdate(userPostScore,updateWrapper);
            }
        });
        recommendService.cacheSimilarUser();
    }
    //将社区统计数据和帖子统计数据的缓存更新到数据库中
    @Scheduled(cron = "0 0/30 * * * ?")//每半小时执行
    public void updateStatisticCache(){
        communityService.saveCacheJoinNum();
        communityService.saveCachePostNum();
        postService.saveCacheCollectNum();
        postService.saveCacheLikedNum();
        postService.saveCacheViewNum();
        postService.saveCacheCommentNum();

    }






}
